package StudyCode.summary.zipfile;

import java.io.*;
import java.util.zip.GZIPOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GzipInputStreamGzippedOutputStream {
	public static void gzip(InputStream is, GZIPOutputStream gzippedOutputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = is.read(buffer)) != -1) {
			gzippedOutputStream.write(buffer, 0, len);
		}
	}


	public static void gzip(InputStream is, File gzippedFile)
			throws IOException {
		GZIPOutputStream gos = null;
		try {
			gos = new GZIPOutputStream(new FileOutputStream(gzippedFile));
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = is.read(buffer)) != -1) {
				gos.write(buffer, 0, len);
			}
		} finally {
			close(gos);
		}
	}


	public static void gzip(File srcFile, File gzippedFile)
			throws IOException {
		GZIPOutputStream gos = null;
		FileInputStream fis = null;
		try {
			gos = new GZIPOutputStream(new FileOutputStream(gzippedFile));
			fis = new FileInputStream(srcFile);
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = fis.read(buffer)) != -1) {
				gos.write(buffer, 0, len);
			}
		} finally {
			close(fis);
			close(gos);
		}
	}

	private static void close(Closeable io) {
		if (io != null) {
			try {
				io.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			io = null;
		}
	}
}
